package com.gibs.peixunapi.service.impl;

import com.gibs.peixunapi.VO.ShowExerciseRecordDTO;
import com.gibs.peixunapi.VO.ShowExerciseRecordVO;
import com.gibs.peixunapi.VO.StudyRecordVO;
import com.gibs.peixunapi.dao.ExerciseRecordDao;
import com.gibs.peixunapi.dao.ExercisesDao;
import com.gibs.peixunapi.dao.UserDao;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.mapper.ExerciseRecordMapper;
import com.gibs.peixunapi.model.ExerciseRecord;
import com.gibs.peixunapi.model.Exercises;
import com.gibs.peixunapi.model.User;
import com.gibs.peixunapi.result.PageData;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.ExerciseRecordService;
import com.gibs.peixunapi.utils.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityExistsException;
import javax.validation.Valid;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
@Slf4j
public class ExerciseRecordServiceImpl implements ExerciseRecordService {

    @Autowired
    private ExerciseRecordDao exerciseRecordDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private ExercisesDao exercisesDao;
    @Autowired
    private ExerciseRecordMapper exerciseRecordMapper;

    @Override
    public Result create(@Valid ExerciseRecord object) {
        try {

//			objectDao.save(object);
            return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result create(Integer studentId, List<StudyRecordVO> recordVOList) {
        for (StudyRecordVO recordVO : recordVOList) {
            ExerciseRecord exerciseRecord = new ExerciseRecord();
            User student = userDao.findById(studentId).orElseThrow(EntityExistsException::new);
            exerciseRecord.setStudent(student);
            Exercises exercises = exercisesDao.findById(recordVO.getExerciseId()).orElseThrow(EntityExistsException::new);
            exerciseRecord.setExercise(exercises);
            exerciseRecord.setMyAnswer(recordVO.getMyAnswer());
            if (recordVO.getMyAnswer().equals(exercises.getCourseSubjectHub().getSubjectHub().getAnswer())) {
                exerciseRecord.setIsRight(true);
            } else {
                exerciseRecord.setIsRight(false);
            }
            exerciseRecordDao.saveAndFlush(exerciseRecord);
        }
        return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
    }

    @Override
    public Result delete(Integer id) {
        try {
//			String[] ids = id.split(",");
//			for (String str : ids) {
//				objectDao.delete(Integer.valueOf(str));
//			}
            return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result getListPage(Integer studentId, Integer courseId, Integer chapterId, String key, Integer current, Integer limit) {
        try {

            ShowExerciseRecordVO s = exerciseRecordMapper.getCourse(studentId, courseId);
            PageHelper.startPage(current, limit);
            PageInfo<ShowExerciseRecordDTO> page = new PageInfo<>(exerciseRecordMapper.getChapterRecord(s.getCourseId(), studentId));

            List<ShowExerciseRecordDTO> list = page.getList();
            s.setRecordDTOList(list);

            PageData<ShowExerciseRecordVO> data = new PageData<>((int) page.getTotal(), page.getPageNum(),
                                            page.getPageSize(), limit, page.getPages(), s);
            return ResultUtil.successPage(ResultEnum.SUCCESS, data);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(ResultEnum.SERVER_ERROR);
        }
    }


}

